import java.util.regex.Matcher;
import java.util.regex.Pattern;

/*
 * 	This class represents the algorithm which extracts staff names from the html
 * 	of the DCU computing stafflist page.
 */
public class DCUStrategy implements Strategy 
{

    public DCUStrategy () 
    {
    }

    public Faculty execute (StringBuffer buff) 
    {
    	Faculty fac = new Faculty();
    	//Pattern to match names in the list
    	Pattern dcu = Pattern.compile(">(\\s)?(Mr|Ms|Mrs|Dr|Prof(essor)?)(\\.)(\\s)(\\w+)(((\\s)|-)\\w+){1,3}<(/a|br)>", Pattern.CASE_INSENSITIVE);
    	//pattern to remove the excess characters
		Pattern removeTags = Pattern.compile("(<(\\/)?(\\w)*>)|>",Pattern.CASE_INSENSITIVE);
		Pattern title = Pattern.compile("(Mr|Ms|Mrs|Dr|Prof(essor)?)((\\.)|(\\s))", Pattern.CASE_INSENSITIVE);
		
		Matcher rough = dcu.matcher(buff);
		Matcher tags = removeTags.matcher(buff);
		Matcher rank = title.matcher(buff);
		
		/*
		 * while there are names left,
		 * remove the excess chars,
		 * save as a new person,
		 * add to institute
		 */
		while(rough.find())
		{
			String str = rough.group();
			tags = removeTags.matcher(str);
			str = tags.replaceAll("");
			rank = title.matcher(str);
			str = rank.replaceAll("");
			Person p = new Person();
			p.setName(str);
			System.out.println(str);
			fac.addStaffMember(p);
		}
        return fac;
    }
    
    public static void main(String [] args)
    {
    	StringBuffer sb = new StringBuffer("<h1 class=\"section_title\"><!-- SECTION HEADING -->Contact Details<!-- SECTION HEADING ENDS --> </h1></div><br /><br /><div class=\"staff_list_computing_staff\"><h3 class=\"phone_role\">Staff</h3><table width=100%  border=\"0\"><tr valign=\"bottom\" class=\"color\"><td class=\"whitesml\"><b>Name</b></td><td class=\"whitesml\"><b>Phone Number</b></td><td class=\"whitesml\"><b>Room</b></td></tr><tr class=\"lightgrey\"><td><a href=\"staff_member.php?id_no=3046\">Dr. Ana Barat</a></td><td>5661/6747</td><td>LG01 Scisysm Office</td></tr><tr><td><a href=\"staff_member.php?id_no=3733\">Dr. Paulo Barreto</a></td><td>1489</td><td>LG01</td></tr><tr class=\"lightgrey\"><td><a href=\"staff_member.php?id_no=140\">Dr Stephen Blott</a></td><td>5984</td><td>L1.22</td></tr><tr><td><a href=\"staff_member.php?id_no=186\">Ms Nora Brophy</a></td><td>5642</td><td>L2.32</td></tr><tr class=\"lightgrey\"><td><a href=\"staff_member.php?id_no=358\">Mr Gary Conway</a></td><td>5648</td><td>L1.08</td></tr><tr><td><a href=\"staff_member.php?id_no=3644\">Dr. Saman Cooray</a></td><td>6830</td><td>L1.29</td></tr><tr class=\"lightgrey\"><td><a href=\"staff_member.php?id_no=390\">Dr Martin Crane</a></td><td>8974</td><td>L2.51</td></tr><tr><td><a href=\"staff_member.php?id_no=413\">Mr Eugene Curran</a></td><td>5941</td><td>L1.10</td></tr><tr class=\"lightgrey\"><td><a href=\"staff_member.php?id_no=425\">Mr Charlie Daly</a></td><td>5572</td><td>L2.29</td></tr><tr><td><a href=\"staff_member.php?id_no=3375\">Dr John Denari</a></td><td>8434</td><td>LG06</td></tr><tr class=\"lightgrey\"><td><a href=\"staff_member.php?id_no=503\">Mr Jim Doyle</a></td><td>5350</td><td>L1.06</td></tr><tr><td><a href=\"staff_member.php?id_no=3506\">Dr. Jinhua Du</a></td><td>6914</td><td>L2.08</td></tr><tr class=\"lightgrey\"><td><a href=\"staff_member.php?id_no=518\">Mr Howard Duncan</a></td><td>5639</td><td>L2.37</td></tr><tr><td><a href=\"staff_member.php?id_no=2922\">Ms Julie Ann Ennis</a></td><td>5237</td><td>L121</td></tr><tr class=\"lightgrey\"><td><a href=\"staff_member.php?id_no=654\">Dr Paul Ferguson</a></td><td>8565</td><td>L1.31</td></tr><tr><td><a href=\"staff_member.php?id_no=3431\">Ms Riona Finn</a></td><td>6707</td><td>End of L2.08</td></tr><tr class=\"lightgrey\"><td><a href=\"staff_member.php?id_no=3052\">Dr Sisay Fissaha Adafre</a></td><td>8063</td><td>L2.08</td></tr><tr><td><a href=\"staff_member.php?id_no=671\">Dr Donal Fitzpatrick</a></td><td>8929</td><td>L2.24</td></tr><tr class=\"lightgrey\"><td><a href=\"staff_member.php?id_no=2365\">Dr Colum Foley</a></td><td>6833</td><td>L1.29</td></tr><tr><td><a href=\"staff_member.php?id_no=2989\">Dr Jennifer Foster</a></td><td>5263</td><td>L2.14</td></tr><tr class=\"lightgrey\"><td><a href=\"staff_member.php?id_no=3620\">Dr. Robert Granger</a></td><td>6743</td><td>L101</td></tr><tr><td><a href=\"staff_member.php?id_no=739\">Dr David Gray</a></td><td>8056</td><td>L2.54</td></tr><tr class=\"lightgrey\"><td><a href=\"staff_member.php?id_no=3374\">Ms Cara Nicole Greene</a></td><td>6704</td><td>L208</td></tr><tr><td><a href=\"staff_member.php?id_no=756\">Dr Cathal Gurrin</a></td><td>5234</td><td>L2.42</td></tr><tr class=\"lightgrey\"><td><a href=\"staff_member.php?id_no=763\">Dr Geoff Hamilton</a></td><td>5017</td><td>L2.55</td></tr><tr><td><a href=\"staff_member.php?id_no=769\">Mr Sean Haran</a></td><td>8703</td><td>L1.05</td></tr><tr class=\"lightgrey\"><td><a href=\"staff_member.php?id_no=790\">Dr Markus Helfert</a></td><td>8727</td><td>L2.26</td></tr><tr><td><a href=\"staff_member.php?id_no=3505\">Dr. Deirdre Hogan</a></td><td>8712</td><td>L2.12</td></tr><tr class=\"lightgrey\"><td><a href=\"staff_member.php?id_no=2961\">Dr. Edmond Holohan</a></td><td>8458</td><td>L2.13</td></tr><tr><td><a href=\"staff_member.php?id_no=827\">Prof Jane Horgan</a></td><td>5260</td><td>L2.02</td></tr><tr class=\"lightgrey\"><td><a href=\"staff_member.php?id_no=891\">Dr Mark Humphrys</a></td><td>8059</td><td>L2.25</td></tr><tr><td><a href=\"staff_member.php?id_no=2966\">Dr. Anna Johnston</a></td><td>8456</td><td>L1.11</td></tr><tr class=\"lightgrey\"><td><a href=\"staff_member.php?id_no=930\">Dr Gareth Jones</a></td><td>5559</td><td>L2.28</td></tr><tr><td><a href=\"staff_member.php?id_no=995\">Mr Gary Keogh</a></td><td>5449</td><td>L2.48</td></tr><tr class=\"lightgrey\"><td><a href=\"staff_member.php?id_no=998\">Ms Jane Kernan</a></td><td>8973</td><td>L2.52</td></tr><tr><td><a href=\"staff_member.php?id_no=1012\">Dr Lynn Killen</a></td><td>5543</td><td>L2.50</td></tr><tr class=\"lightgrey\"><td><a href=\"staff_member.php?id_no=3514\">Dr Patrik Lambert</a></td><td>6914</td><td>L208</td></tr><tr><td><a href=\"staff_member.php?id_no=1071\">Dr Hyowon Lee</a></td><td>5829</td><td>L2.46</td></tr><tr class=\"lightgrey\"><td><a href=\"staff_member.php?id_no=3645\">Dr Johannes Leveling</a></td><td>6716</td><td>L208</td></tr><tr><td><a href=\"staff_member.php?id_no=3749\">Mr Milos Maryska</a></td><td></td><td>L2.23</td></tr><tr class=\"lightgrey\"><td><a href=\"staff_member.php?id_no=3466\">Ms Eithne McCann</a></td><td>6700</td><td>L206</td></tr><tr><td><a href=\"staff_member.php?id_no=1233\">Mr Peter McGorman</a></td><td>5359</td><td>L2.38</td></tr><tr class=\"lightgrey\"><td><a href=\"staff_member.php?id_no=1249\">Dr John McKenna</a></td><td>5507</td><td>L2.47</td></tr><tr><td><a href=\"staff_member.php?id_no=1343\">Prof Joe Morris</a></td><td>8419</td><td>L1.13</td></tr><tr class=\"lightgrey\"><td><a href=\"staff_member.php?id_no=3426\">Dr. Sara Morrissey</a></td><td>6711</td><td>L2.08</td></tr><tr><td><a href=\"staff_member.php?id_no=2985\">Mr James Murphy</a></td><td>6741</td><td>LG01 Scisym</td></tr><tr class=\"lightgrey\"><td><a href=\"staff_member.php?id_no=1362\">Dr John Murphy</a></td><td>5363</td><td>L2.33</td></tr><tr><td><a href=\"staff_member.php?id_no=3251\">Ms Shiofra Murphy</a></td><td>8980</td><td>L1.21</td></tr><tr class=\"lightgrey\"><td><a href=\"staff_member.php?id_no=1434\">Dr Darragh O'Brien</a></td><td>5645</td><td>L2.35</td></tr><tr><td><a href=\"staff_member.php?id_no=3429\">Mr Maurice O'Connor</a></td><td>8448</td><td>PG Bay A</td></tr><tr class=\"lightgrey\"><td><a href=\"staff_member.php?id_no=1467\">Dr Rory O'Connor</a></td><td>5643</td><td>L2.30</td></tr><tr><td><a href=\"staff_member.php?id_no=1493\">Dr Neil O'Hare</a></td><td>6007</td><td>N207</td></tr><tr class=\"lightgrey\"><td><a href=\"staff_member.php?id_no=3379\">Dr Sylwia Ozdowska</a></td><td>6340</td><td>L203</td></tr><tr><td><a href=\"staff_member.php?id_no=1565\">Dr Claus Pahl</a></td><td>5620</td><td>L2.27</td></tr><tr class=\"lightgrey\"><td><a href=\"staff_member.php?id_no=3519\">Dr. Carlos Andre Reis Pinheiro</a></td><td>8456</td><td>L2.32</td></tr><tr><td><a href=\"staff_member.php?id_no=1701\">Dr Mark Roantree</a></td><td>5636</td><td>L2.34</td></tr><tr class=\"lightgrey\"><td><a href=\"staff_member.php?id_no=3430\">Mr Jeffrey Roe</a></td><td>8448</td><td>PG Bay A</td></tr><tr><td><a href=\"staff_member.php?id_no=1735\">Prof Heather Ruskin</a></td><td>5513</td><td>LG01</td></tr><tr class=\"lightgrey\"><td><a href=\"staff_member.php?id_no=1748\">Prof Michael Ryan</a></td><td>5238</td><td>LG 02</td></tr><tr><td><a href=\"staff_member.php?id_no=2963\">Dr. Christian Schellewald</a></td><td>5936</td><td>L2.11</td></tr><tr class=\"lightgrey\"><td><a href=\"staff_member.php?id_no=1773\">Prof Mike Scott</a></td><td>5537</td><td>LG01</td></tr><tr><td><a href=\"staff_member.php?id_no=3388\">Dr. Paraic Sheridan</a></td><td>6706</td><td>End of L208</td></tr><tr class=\"lightgrey\"><td><a href=\"staff_member.php?id_no=1809\">Dr David Sinclair</a></td><td>5510</td><td>L2.53</td></tr><tr><td><a href=\"staff_member.php?id_no=3383\">Prof. Harold Somers</a></td><td> 6703</td><td>L208</td></tr><tr class=\"lightgrey\"><td><a href=\"staff_member.php?id_no=3378\">Dr Annette Stahl</a></td><td>5936</td><td>L2.11</td></tr><tr><td><a href=\"staff_member.php?id_no=1863\">Mr Brian Stone</a></td><td>5920</td><td>L2.44</td></tr><tr class=\"lightgrey\"><td><a href=\"staff_member.php?id_no=1873\">Dr Alistair Sutherland</a></td><td>5511</td><td>L1.04</td></tr><tr><td><a href=\"staff_member.php?id_no=3400\">Dr. Lamia Tounsi</a></td><td>6905</td><td>L2.08</td></tr><tr class=\"lightgrey\"><td><a href=\"staff_member.php?id_no=1921\">Dr Liam Tuohey</a></td><td>8728</td><td>L2.36</td></tr><tr><td><a href=\"staff_member.php?id_no=1935\">Prof Josef Van Genabith</a></td><td>6700</td><td>L2.08</td></tr><tr class=\"lightgrey\"><td><a href=\"staff_member.php?id_no=1939\">Mr Renaat Verbruggen</a></td><td>5257</td><td>L2.43</td></tr><tr><td><a href=\"staff_member.php?id_no=2957\">Dr. Liviu-Mihai Vladutu</a></td><td>8291</td><td>L1.12</td></tr><tr class=\"lightgrey\"><td><a href=\"staff_member.php?id_no=1947\">Mr Joachim Wagner</a></td><td>6700</td><td>L208</td></tr><tr><td><a href=\"staff_member.php?id_no=1961\">Mr Ray Walshe</a></td><td>5597</td><td>LG01</td></tr><tr class=\"lightgrey\"><td><a href=\"staff_member.php?id_no=1965\">Ms Monica Ward</a></td><td>8719</td><td>L1.09</td></tr><tr><td><a href=\"staff_member.php?id_no=1973\">Prof Andy Way</a></td><td>5644</td><td>L2.01</td></tr></table>	</div></div><!-- CONTENTS END --><!-- List of links --><div id=");
    	Faculty jim = new Faculty();
    	Context con = new Context(new DCUStrategy());
    	jim = con.execute(sb);
    	jim.printAll();    	
    }

}